package com.tianyi.comUserInfo;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.tianyi.dao.entity.comUser.ComUser;
import com.tianyi.dao.entity.comUser.ComUserExample;
import com.tianyi.dao.entity.comUserInfo.ComUserInfo;
import com.tianyi.dao.entity.comUserInfo.ComUserInfoExample;
import com.tianyi.dao.entity.company.Company;
import com.tianyi.dao.mapper.ComUserInfoMapper;
import com.tianyi.dao.mapper.ComUserMapper;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.BaseReq;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * com user service
 *
 * @author cuixuhui
 * @date 2020/05/03
 */
@Service
public class ComUserInfoService {

    private static final Logger logger = LoggerFactory.getLogger(ComUserInfoService.class);

    @Autowired
    private ComUserInfoMapper comUserInfoMapper;

    public ComUserInfo queryComUserInfoByParentId(Long parentId){

        ComUserInfoExample example = new ComUserInfoExample();
        example.createCriteria().andComUserIdEqualTo(parentId);
        List<ComUserInfo> comUserInfos = comUserInfoMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(comUserInfos)){
            return null;
        }
        return comUserInfos.get(0);
    }

    public void insertComUserInfo(ComUserInfo comUserInfo) {
        if (Objects.isNull(comUserInfo)){
            return;
        }
        comUserInfoMapper.insert(comUserInfo);
    }

    public void updateComUserInfoByPrimaryKeySelective(ComUserInfo comUserInfo) {

        if (Objects.isNull(comUserInfo)){
            return;
        }
        comUserInfoMapper.updateByPrimaryKeySelective(comUserInfo);
    }

    public Map<Long, String> queryJobsMapByComUserIds(List<Long> comUserIds) {

        List<ComUserInfo> comUserInfos = queryComUserInfosByComUserIds(comUserIds);

        return ListUtils.emptyIfNull(comUserInfos).stream().collect(Collectors.toMap(ComUserInfo::getId, ComUserInfo::getJobs, (oldOne, newOne) -> newOne));
    }

    public List<ComUserInfo> queryComUserInfosByComUserIds(List<Long> comUserIds) {

        ComUserInfoExample example = new ComUserInfoExample();
        example.createCriteria().andComUserIdIn(comUserIds);
        List<ComUserInfo> comUserInfos = comUserInfoMapper.selectByExample(example);
        return comUserInfos;
    }

    /**
     * 根据关键字模糊搜索 用户详细信息
     * @param keyword
     * @return
     */
    public List<Long> queryComUserInfoByKeyword(String keyword){

        if (StringUtils.isBlank(keyword)){
            return Lists.newArrayList();
        }
        ComUserInfoExample example = new ComUserInfoExample();
        example.createCriteria().andRealNameLike("%"+keyword+"%");
        example.or().andWechatNumLike("%"+keyword+"%");
        List<ComUserInfo> comUserInfos = comUserInfoMapper.selectByExample(example);
        return ListUtils.emptyIfNull(comUserInfos).stream().map(ComUserInfo::getComUserId).distinct().collect(Collectors.toList());
    }

    /**
     * 删除用户详情信息
     * @param id
     */
    public void deleteComUserInfoById(Long id) {
        if (Objects.isNull(id)){
            return;
        }
        comUserInfoMapper.deleteByPrimaryKey(id);
    }
}
